﻿using Microsoft.EntityFrameworkCore;
using OnlineCourse.Model.Entity;
using OnlineCourse.Model.EntityConfiguration;


namespace OnlineCourse.Model
{
    public class ApplicationDbContext : DbContext//（实体类或域与数据库的桥梁）
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
            Database.Migrate();//如果没有数据库就创建可迁移的数据库  文档：https://docs.microsoft.com/en-us/ef/core/
                               //Add-Migration该命令会更新模型的快照，并且创建迁移主迁移文件
                               //Remove-Migration
                               //Update-Database执行迁移
        }
        public DbSet<User> Users { get; set; }//定义DBset类型，用来查询和保存实例。可以用于创建，读取，更新，删除操作的实体集
        public DbSet<Teacher> Teachers { get; set; }
        public DbSet<Section> Sections { get; set; }
        public DbSet<PayOrder> PayOrders { get; set; }
        public DbSet<PaymentType> PaymentTypes { get; set; }
        public DbSet<LikeCollection> LikeCollections { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<CouponsRecord> CouponsRecords { get; set; }
        public DbSet<Coupon> Coupons { get; set; }
        public DbSet<Comment> Comments { get; set; }
        public DbSet<BuyOrder> BuyOrders { get; set; }
        public DbSet<Advertising> Advertisings { get; set; }
        public DbSet<CouponCourse> CouponCourses { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)//将实体映射到数据表
        {
            builder.ApplyConfiguration(new UserConfiguration());//配置用户
            builder.ApplyConfiguration(new CouponConfiguration());//配置优惠券
            builder.ApplyConfiguration(new CouponsRecordConfiguration());//配置领券记录
            builder.ApplyConfiguration(new PaymentTypeConfiguration());//配置支付方式
            builder.ApplyConfiguration(new CourseConfiguration());//配置课程
            base.OnModelCreating(builder);
        }
    }
}
